PROGRAM	SMPlaySounds;
{Cameron Birse, Macintosh Developer Tech Support}
{$load PlayLoad}
USES
    Memtypes,QuickDraw,OSIntf,ToolIntf,PackIntf,Sound;


CONST
      TextStatItem = 2;   {Item number of static text}
      textItem = 3;        {item number for editable text item}

TYPE
    SampBuffer = RECORD
    PtrToData :     ptr;
    NumSamps  :     longint;
    SampRate  :     fixed;
    AltSampLoop:    longint;
    EndSampLoop:    longint;
    baseNote  :     integer;
    samples   :     ARRAY [0..10000] OF byte
                 END;
    SampBufPtr = ^SampBuffer;
    
VAR
    err : integer;
	 		DialRect : Rect;
	 		DialPtr : DialogPtr;
		 	DialTitle,GuessStr,RealStr : str255;
	 		isVis, DialGoAway : boolean;
	 		DialRefCon : longint;
	 		DialItemHit, DITLID : integer;
	 		DITLHndl : handle;
    CloseWind,PassWrdOK :boolean;
    ResHell : integer;
    theType : integer;   {gives the type of the item requested}
    theTextHdl : handle;    {gives a handle to the item}
    txtBox : Rect;    {gives the display rectangle of the item}
    PBlkPtr     : ParmBlkPtr;
    Pblk        : ParamBlockRec;
    VolName : str255;
    VolRefN, FilRefN : integer;
    MyBuffer : SampBufPtr;
    BigBuf,HowMuch,HowMany : longint;
    SampChnPtr : SndChannelPtr;
    SoundAway, NoMore, NoWait : boolean;
    BufPtr : ptr;
    MyCommand : SndCommand;
	myEvent : eventRecord;


		{------------------------------------------------------------------------------------}

		PROCEDURE InitMac;

			BEGIN					   {InitMac}

   InitGraf (@thePort);          {the big five inits}
   InitFonts;
   InitWindows;
   TEInit;
   InitDialogs (nil);

			END;					   {InitMac}

		{------------------------------------------------------------------------------------}

PROCEDURE DoError (ErStr : str255; err : longint);

VAR
    NumStr : str255;
	getOut : boolean;

    Begin
		if err <> 0 then NumToString (err,NumStr)
		else NumStr := '';
        ErStr := Concat (ErStr,NumStr);
        DITLID := 257;
        DITLHndl := GetResource ('DITL', DITLID);
        ResHell := ResError;
        if ResHell = noerr then
        begin
        	if DITLHndl <> nil then

        	begin
            	Hlock (DITLHndl);

            	DialPtr := GetNewDialog (257,nil,WindowPtr(-1));
            	If DialPtr <> nil then
            	begin
            		GetDItem (DialPtr, TextStatItem, theType, theTextHdl, txtBox);
            		If theTextHdl <> nil then
            		begin
     
            			SetPort (DialPtr);

            			SetIText (theTextHdl, ErStr);
						getOut := false;
						repeat
						if GetNextEvent (everyEvent,myEvent) then
						begin
							if IsDialogEvent (myEvent) then
							begin
								if DialogSelect (myEvent,DialPtr,DialItemHit) then
								begin
									Case DialItemHit of  {1 = OK btn, 2 = message}
   	             				1,2 : begin
											CloseDialog (DialPtr);
											getOut := true;
										  end;
									end; {case}
								end;
							end;
						end;
						until getOut;
            		end;
            	end;
        	end;
        end;
		If DITLHndl <> nil then HUnLock (DITLHndl);
     end;

		{------------------------------------------------------------------------------------}
    
PROCEDURE AllDone;

    Begin
        SetUpA5;
        SoundAway := true;
        RestoreA5;
    End;


		{------------------------------------------------------------------------------------}

Procedure PlaySound;

CONST
    opWrEr = -49;

VAR
    MyReply : SFReply;
    Place : point;
    MyTypeList : SFTypeList;
    finalTicks : LongInt;
    PlayPtr : ptr;
    
    Begin
        
        Place.v := 50;
        Place.h := 50;
        NoWait := true;
        
            SFGetFile (Place,'',nil,-1,MyTypeList,nil,MyReply);
            if MyReply.good then
            begin
                err := FSOpen (MyReply.fname, MyReply.vRefNum, FilRefN);
                if err = noerr then
                begin
                    err := GetEOF(FilRefN,HowMany);
					if HowMany < BigBuf then
					begin
                        MyCommand.cmd := quietCmd;
                        MyCommand.param1 := 0;
                        MyCommand.param2 := 0;
                        err := SndDoImmediate (SampChnPtr,MyCommand);
                        MyCommand.cmd := flushCmd;
                        MyCommand.param1 := 0;
                        MyCommand.param2 := 0;
                        err := SndDoImmediate (SampChnPtr,MyCommand);
                        if err = noerr then
						begin
                    		err := FSRead (FilRefN, HowMany, ptr(@MyBuffer^.samples));
                    		if err = opWrEr then err := noerr;
                    		if err = eofErr then err := noerr;
                    		if err = noerr then
                    		begin
								MyBuffer^.PtrToData := nil;
                        		MyBuffer^.NumSamps := HowMany;
                        		MyBuffer^.SampRate := FixRatio(22000,1);
                        		MyBuffer^.AltSampLoop := 0;
                        		MyBuffer^.EndSampLoop := 0;
                        		MyBuffer^.baseNote := 60;
                        		soundAway := false;
                        		MyCommand.cmd := bufferCmd;
                        		MyCommand.param1 := 0;
                        		MyCommand.param2 := longint(MyBuffer);
                        		err := SndDoImmediate (SampChnPtr,MyCommand);
                        		if err <> noerr then
                        		begin
                            		DoError ('SndDoCommand (bufferCmd) err = ',longint(err));
                        		end;
                    		end
                    		else
                    		begin
                        	DoError ('FSRead Error = ',longint(err));
                    		end;
						end
						else DoError ('SndDoImmed (flushCmd) error ',err);
					end
					else DoError ('File Too Big, Sam',0);
                end
                else
                begin
                    DoError ('FSOpen Error = ', longint(err));
                end;
            end
            else
            begin
                NoMore := true;
            end;
        err := FSClose (FilRefN);
     end;

		{------------------------------------------------------------------------------------}

	
    begin {main DudeFace}
      
        initmac;
        MaxApplZone;
        HowMuch := FreeMem;
        DoError ('Free Memory = ',longint(HowMuch));

        NoMore := False;
        SoundAway := true;

	  	MyBuffer := SampBufPtr(newPtr (HowMuch-8192));
        err := MemError;
        If MyBuffer <> nil then
        begin
		    err := SndNewChannel(SampChnPtr,5,$00,nil);
        	if err = noerr then
        	begin
            repeat
                BigBuf := GetPtrSize (ptr(MyBuffer));
                PlaySound;
				if noMore = false then DoError ('Press mouse for more',0);
            until noMore;
			end
			else doError ('SndNewChannel error = ',longint(err));
			err := sndDisposeChannel (SampChnPtr,true);
        end
        else
        begin
            DoError ('NewPtr Nil Handle, err ',longint(err));
        end;
        
        if MyBuffer <> nil then disposPtr (ptr(MyBuffer));
        
    end.